package com.roger.myHome.interceptor;

import com.roger.myHome.util.Constants;
import com.roger.myHome.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AuthorityInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String header = request.getHeader(Constants.HEADER);

        String method = request.getMethod();
        String uri = request.getRequestURI();

        boolean result = checkUri(uri.toUpperCase(), header);
        //System.out.println(uri.toUpperCase() + ":" + result);
//        if (!result) {
//            System.out.println("=====");
//            System.err.println("请求uri:" + uri + ",header:" + header);
//        }
        if (header != null && !"".equalsIgnoreCase(header)) {
            Claims claims = JwtUtil.checkToken(header);
            if (claims == null) {
                System.out.println(header);
                try{
                    String path = request.getContextPath();
                    String basePath = request.getScheme() + "://" + request.getServerName()
                            + ":" + request.getServerPort() + path + "/";
                    response.reset();
                    response.setContentType("text/html;charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.println("<html>");
                    out.println("<script>");
                    out.println("window.open('"+basePath+"','_parent')");
                    out.println("</script>");
                    out.println("</html>");
                }catch(Exception e){
                    System.err.println(e.getMessage());
                }
                return false;
            }
            System.out.println("login user id:" + claims.get(Constants.USER_ID));
        }
        return result;
    }

    private static List<String> URI_LIST = new ArrayList();

    static {
        URI_LIST.add("/LOGIN");
        URI_LIST.add("/INDEX");
        URI_LIST.add("/NAVIGATOR");
        URI_LIST.add("/TOKEN/REFRESH");
        URI_LIST.add("/BLOODSUGAR/EXPORT");
        URI_LIST.add("/ASSETS/BUYTICKET");
        URI_LIST.add("/EXCEL/IMPORT");
    }

    private static boolean checkUri(String uri, String token) {
        if (uri.endsWith(".JS")
                || uri.endsWith(".HTML")
                || uri.endsWith(".CSS")
                || uri.endsWith(".PNG")
                || uri.endsWith(".GIF")
                || uri.contains("CACHE")
                || uri.contains("REFRESH")) {
            return true;
        } else if (URI_LIST.contains(uri)){
            return true;
        } else if(token == null || "".equals(token)) {
            return false;
        } else {
            return true;
        }
    }

}
